<template>
  <div class="app-container">
    <el-card class="box-card search-div" v-show="showSearch">
      <el-form :inline="true" :model="listQuery" ref="queryForm">
        <el-form-item label="表单名称">
          <el-input placeholder="请输入查询的表单名称" v-model="listQuery.name"/>
        </el-form-item>
        <el-button type="primary" @click="handleSearch" icon="el-icon-search">搜索</el-button>
        <el-button @click="handleReset" icon="el-icon-refresh">重置</el-button>
      </el-form>
    </el-card>
    <el-card class="box-card">
      <div slot="header" class="clearfix">
        <el-button type="primary" v-hasPermi="['flowable_form_add']" class="el-icon-plus" @click="add">新增</el-button>
        <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
      </div>
      <el-table :data="list" v-loading.body="listLoading" border fit highlight-current-row class="table-list">
        <el-table-column align="center" label="表单名称" prop="name"/>
        <el-table-column align="center" label="表单key" prop="formKey">
        </el-table-column>
        <el-table-column align="center" label="描述" prop="remark" :show-overflow-tooltip="true">
        </el-table-column>
        <el-table-column align="center" label="更新时间" prop="updateTime" width="150">
        </el-table-column>
        <el-table-column align="center" label="操作" width="260" fixed="right">
          <template slot-scope="scope">
            <el-button plain v-hasPermi="['flowable_form_edit']" type="violet" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
            <el-button plain v-hasPermi="['flowable_form_desgin']" @click="editForm(scope.row)" type="success">表单设计</el-button>
            <el-button plain v-hasPermi="['flowable_form_remove']" type="danger" @click="removeByIds(scope.row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <pagination v-show="total>0" :total="total" :page.sync="listQuery.page" :limit.sync="listQuery.limit" @pagination="getList" />
    </el-card>
    <!--弹出框-->
    <el-dialog :title="dialogTitle" :width="'600px'" :visible.sync="showDialog" :close-on-click-modal="false">
      <el-form :rules="rules" ref="saveFormData" :model="saveFormData" label-width="80px">
        <el-form-item label="表单名称" prop="name">
          <el-input v-model="saveFormData.name" :maxlength="20" placeholder="最多输入10个字符" clearable show-word-limit></el-input>
        </el-form-item>
        <el-form-item label="表单key" prop="formKey">
          <el-input v-model="saveFormData.formKey" :maxlength="10" clearable show-word-limit></el-input>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="saveFormData.remark" :rows="2" type="textarea" maxlength="100" placeholder="最多输入100个字符" clearable show-word-limit></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="showDialog = false">取 消</el-button>
        <el-button type="primary" @click="onSave('saveFormData')">确 定</el-button>
      </div>
    </el-dialog>

    <el-dialog title="自定义表单"
          append-to-body
          modal
          center
          fullscreen
          :visible.sync="formEditDialog">

        <FormDesignTCD ref="fdt" @save="handleSave" :defaultValue="defaultValue"/>
    </el-dialog>
  </div>
</template>

<script>
import * as form from './js/flowForm'
import { mapGetters } from 'vuex'

export default {
  name: 'flowableFormList',
  data() {
    return {
      showSearch:true,
      list: null,
      listLoading: true,
      total: 0,
      listQuery: {
        page: 1, limit: 10, name: '', status: ''
      },
      formEditDialog:false,
      showDialog: false,
      saveFormData: {
        id: '', name: '',formKey:'',formSchema:'', remark: '', orderNum: ''
      },
      rules: {
        name: [{ required: true, message: '请输入表单名称', trigger: 'blur' }
        ]
        ,formKey:[{ required: true, message: '请输入表单key', trigger: 'blur' }]
      },
      dialogTitle: '添加流程表单',
        defaultValue:{
      },
      fdtDefaultValue:{
        config: {
                labelPosition: "right",
                labelWidth: 15,
              },
        list: []
      }
    }
  },
  computed: {
    ...mapGetters(['permissions'])
  },
  created() {
    this.getList()
  },
  methods: {
    getList() { // 获取所有流程表单
      this.listLoading = true
      form.fetchList(this.listQuery).then(response => {
        this.list = response.data.records
        this.total = response.data.total
        this.listLoading = false
      })
    },
    handleSearch() {
      this.listQuery.page = 1
      this.getList()
    },
    handleReset(){
     this.listQuery = {
        page: 1, limit: 10, name: ''
      }
      this.getList()
    },
    handleSizeChange(val) {
      this.listQuery.limit = val
      this.getList()
    },
    handleCurrentChange(val) {
      this.listQuery.page = val
      this.getList()
    },
    //删除
    removeByIds(id) {
      this.$confirm('请确定是否删除该流程表单?', '提示', {
        confirmButtonText: '确定', cancelButtonText: '取消', type: 'warning'
      }).then(() => {
        form.removeByIds(id).then((response) => {
          this.$message({ type: 'success', message: '操作成功!' })
          this.getList()
        })
      }).catch(function(error) { console.log(error) })
    },
    // form 表单操作
    // 新增
    add(pit, index) {
      this.showDialog = true
      this.dialogTitle = '添加流程表单'
      // 清空表单数据
      this.$nextTick(() => {
        this.resetForm('saveFormData')
        this.saveFormData.id = ''
      })
    },
    // 编辑
    handleEdit(index, row) {
      this.showDialog = true
      this.dialogTitle = '编辑流程表单'
      this.$nextTick(() => {
        this.resetForm('saveFormData')
        this.saveFormData.id = row.id
        this.saveFormData.name = row.name
        this.saveFormData.formKey = row.formKey
        this.saveFormData.remark = row.remark
      })
    },
    // 重置表单 解决验证提示的问题
    resetForm(formName) {
      this.$refs[formName].resetFields()
    },
    // 保存
    onSave(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          form.save(this.saveFormData).then((response) => {
            this.showDialog = false
            this.$message({ type: 'success', message: '操作成功!' })
            this.getList()
          }).catch(function(error) {
            console.log(error)
          })
        }
      })
    },
    editForm(row) {
        this.formEditDialog = true
        this.curRow = row
        if(row.formSchema){
          if(this.$refs.fdt){
            this.$refs.fdt.setDesignData(JSON.parse(row.formSchema))
          }else{//首次没有加载只能是这种赋值
            this.defaultValue = JSON.parse(row.formSchema)
          }
        }else{
            this.defaultValue = this.fdtDefaultValue
        }
      },
      handleSave(values){
        this.curRow.formSchema = JSON.stringify(values)
        form.save(this.curRow).then(res => {
          this.formEditDialog = false
          this.getList()
          this.$message({
              type: "success",
              message: "操作成功!"
            });
        });
      }
  }
}
</script>
